﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;

using Homework.Domain.Dto;
using MySql.Data.MySqlClient;
using Dapper;
using Homework.Domain.Table;
using Org.BouncyCastle.Crypto.Tls;

namespace Homework.API.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class UserController : ControllerBase
    {
        public IConfiguration _configuration;

        public UserController(IConfiguration configuration)
        {
            _configuration = configuration;
        }
        /// <summary>
        /// 用户添加
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("UserAdd")]
        public IActionResult UserAdd(UserDto user)
        {
            string conn = _configuration.GetConnectionString("conn");
            using var db = new MySqlConnection(conn);
            string sql = "insert into tb_User values(null,@Name,@Password)";
            int result = db.Execute(sql, new { Name = user.UserName, Password = MD5Hash.Hash.GetMD5(user.Password) });
            return Ok(conn);
        }
        [HttpPost]
        public IActionResult Login(UserDto user)
        {
            string conn = _configuration.GetConnectionString("conn");
            using var db = new MySqlConnection(conn);
            string sql = "select count(1) from tb_User where username=@name and password=@pwd";
            int result = db.ExecuteScalar<int>(sql, new { name = user.UserName, pwd = user.Password });
            return Ok(conn);
        }
    }
}
